<?php
/**
 * 产品提货记录/日志
 * (model)
 */
namespace App\Models\ProductPickup;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class PickupOrderLog extends Model{

    protected $table = 'product_pickup_order_log';

    protected $guarded = [];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    /**
     * 获取提货单号下的当期的sku
     * @param $pickup_no
     * @param null $cycle
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getCycleSku($pickup_no, $cycle = null){
        $query = self::query();
        if ($pickup_no) {
            $query->where('pickup_no', $pickup_no);
        }
        if ($cycle) {
            $query->where('cycle', $cycle);
        }

        return $query->get();
    }

}
